package com.atguigu.distributed.lock.leecode;

public class QuickSort {
    public static void main(String[] args) {
        int[] arr = {1,9,8,7,6,90,89,91,92};
        QuickSort quickSort = new QuickSort();
        quickSort.quickSort(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
    public void quickSort(int[] arr){
        quickSort(arr,0,arr.length-1);
    }
    public void quickSort(int[] arr,int left,int right){
        if(left<right){
            int pivot = partition(arr,left,right);
            quickSort(arr,0,pivot-1);
            quickSort(arr,pivot+1,right);
        }
    }
    public int partition(int[] arr,int left,int right){
        int le = left+1;
        int ge = right;
        while (true){
            while (le<=ge&&arr[le]<arr[left]){
                le++;
            }
            while (le<=ge&&arr[ge]>arr[left]){
                ge--;
            }
            if(le>=ge){
                break;
            }
            swap(arr,le,ge);
            le++;
            ge--;
        }
        swap(arr,left,ge);
        return ge;
    }
    public void swap(int[] arr,int a,int b){
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }
}
